package chaoyue.study.jdbc;

import chaoyue.study.jdbc.utils.DbUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle;

/**
 * 使用Statement执行查询操作
 * 通过ResultSet来获取结果集并迭代处理
 */
public class Test2_Statement2 {
    public static void main(String[] args) throws Exception {
        Connection connection = DbUtils.getConnection();

        // 获取statement
        Statement statement = connection.createStatement();
        String sql = "select id,username,password,birthday from test_user";
        // 执行查询操作 获取结果集 ResultSet
        ResultSet resultSet = statement.executeQuery(sql);
        // 读取第一条数据
        resultSet.next(); // 指针后移
        System.out.println("id=" + resultSet.getInt("id"));
        System.out.println("username=" + resultSet.getString("username"));
        System.out.println("password=" + resultSet.getString("password"));
        System.out.println("birthday=" + resultSet.getString("birthday"));

        // 读取第二条数据
        resultSet.next();// 指针后移
        System.out.println("id=" + resultSet.getInt("id"));
        System.out.println("username=" + resultSet.getString("username"));
        System.out.println("password=" + resultSet.getString("password"));
        System.out.println("birthday=" + resultSet.getString("birthday"));
    }
}
